<template>
  <el-dialog :title="$t('financeModule.list.model.title')" :visible.sync="showState" :before-close="handleClose" :close-on-click-modal="false">
    <el-form ref="formValidate" :model="formValidate" :rules="rules" label-width="120px" class="demo-ruleForm">
      <el-form-item :label="$t('financeModule.list.model.status')" prop="paymentResult">
        <el-select v-model="formValidate.paymentResult" :placeholder="$t('financeModule.list.model.choseStatus')">
          <el-option
            v-for="item in $t('financeModule.list.paymentResult')"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-if="formValidate.paymentResult === 1" :label="$t('financeModule.list.channel')" prop="paymentChannel">
        <el-select v-model="formValidate.paymentChannel" :placeholder="$t('financeModule.list.prompt.channel')">
          <el-option
            v-for="item in $t('financeModule.list.paymentChannel')"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-if="formValidate.paymentResult === 1" :label="$t('financeModule.list.paymentAccoun')" prop="ourAccount">
        <el-input v-model="formValidate.ourAccount" clearable :placeholder="$t('financeModule.list.prompt.paymentAccoun')" :maxlength="50" />
      </el-form-item>
      <el-form-item v-if="formValidate.paymentResult === 1" :label="$t('financeModule.list.serialNumber')" prop="serialNumber">
        <el-input v-model="formValidate.serialNumber" clearable :placeholder="$t('financeModule.list.prompt.serialNumber')" :maxlength="50" />
      </el-form-item>
      <el-form-item v-if="formValidate.paymentResult === 1" :label="$t('financeModule.list.model.payDate')" prop="payDate">
        <el-date-picker
          v-model="formValidate.payDate"
          type="datetime"
          :placeholder="$t('financeModule.list.model.payDate')"
          style="width: 100%"
        />
      </el-form-item>
      <el-form-item v-if="formValidate.paymentResult === 1" :label="$t('financeModule.list.model.payMoney')" prop="payMoney">
        <el-input
          v-model="formValidate.payMoney"
          readonly
          style="width: 100%"
        />
      </el-form-item>
      <el-form-item :label="$t('financeModule.list.model.remark')" :prop="formValidate.paymentResult === 1 ? '' : 'option'">
        <el-input
          v-model="formValidate.option"
          type="textarea"
          :autosize="{ minRows: 4, maxRows: 6}"
          :placeholder="$t('common.input')"
          :maxlength="500"
          show-word-limit
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.operate.cancel') }}</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleSubmit">{{ $t('common.operate.sure') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { confirmLoan } from '@/api/finance'
export default {
  name: 'LoanListDialog',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    chooseItem: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
      submitLoading: false,
      formValidate: {
        orderId: null,
        balanceApplyId: null,
        paymentResult: null,
        paymentChannel: null,
        ourAccount: null,
        serialNumber: null,
        payDate: null,
        payMoney: null,
        option: null
      },
      rules: {
        paymentResult: [{ required: true, message: this.$t('financeModule.list.model.status') + this.$t('common.null'), trigger: 'blur' }],
        paymentChannel: [{ required: true, message: this.$t('financeModule.list.channel') + this.$t('common.null'), trigger: 'blur' }],
        ourAccount: [{ required: true, message: this.$t('financeModule.list.paymentAccoun') + this.$t('common.null'), trigger: 'blur' }],
        serialNumber: [{ required: true, message: this.$t('financeModule.list.serialNumber') + this.$t('common.null'), trigger: 'blur' }],
        payDate: [{ required: true, message: this.$t('financeModule.list.model.payDate') + this.$t('common.null'), trigger: 'blur' }],
        payMoney: [{ required: true, message: this.$t('financeModule.list.model.payMoney') + this.$t('common.null'), trigger: 'blur' }],
        option: [{ required: true, message: this.$t('financeModule.list.model.remark') + this.$t('common.null'), trigger: 'blur' }]
      }
    }
  },
  computed: {
    showState: {
      get: function() {
        return this.showDialog
      },
      set: function() {
      }
    }
  },
  mounted() {
    this.formValidate = this.chooseItem
  },
  methods: {
    handleClose() {
      this.$emit('change-dialog', false)
    },
    handleSubmit() {
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          if (this.formValidate.paymentResult === 1) {
            const payDate = (new Date(this.formValidate.payDate)).getTime()
            const now = (new Date()).getTime()
            if (payDate > now) {
              this.$message.error(this.$t('common.tips.diffTime'))
              return
            }
          }
          this.submitLoading = true
          confirmLoan(this.formValidate)
            .then(res => {
              this.submitLoading = false
              if (res.success) {
                this.$message.success(this.$t('common.tip.editSuccess'))
                this.$emit('change-dialog', false)
                this.$emit('edit-success')
                this.handleClose()
              } else {
                this.$message.error(res.message)
              }
            })
            .catch(err => {
              this.$message.error(err.message)
              this.submitLoading = false
            })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-select{
    width: 100%;
  }
  .el-aside{
    text-align: center;
    padding: 20px 5px
  }
</style>
